const TOOLBAR =[
	{content:"刷题", icon:"el-icon-document-add",type:"primary"},
];
const COLUMNS = [{
		prop: "name",
		label: "姓名",
		width: "80"
	},
	{
		prop: "identityCard",
		label: "身份证",
		width: "160"
	},
	{
		prop: "testname",
		label: "试卷名称",
		width: "80"
	},
	{
		prop: "score",
		label: "得分",
		width: "80"
	},
	{
		prop: "testType",
		label: "试卷类型",
		width: "80"
	},
	{
		prop: "passMark",
		label: "通过得分",
		width: "130"
	},
	{
		prop: "totalScore",
		label: "总分",
		width: "80"
	},
	{
		prop: "questionTotal",
		label: "题目数量",
		width: "80"
	},
	{
		prop: "testNumber",
		label: "考试次数",
		width: "130"
	},
	{
		prop: "add_time",
		label: "添加时间",
		width: "120"
	},
	{
		prop: "update_time",
		label: "更新时间",
		width: "120"
	}
];
const editrules = {
	score: [{
		// required: true,
		// message: '请输入分数',
		// trigger: 'blur'
	}]
}

let app = new Vue({
	el: "#app",
	data() {
		return {
			rightornotflag:"对",
			typeoptions: [],
			teamLink: "i am copy",
			leftbar_show: true,
			// 添加、编辑标识
			action: "",
			tableHeight: window.innerHeight - 180,

			tablecolums: COLUMNS,
			submitDisable: false,
			dialogFormVisible: false,
			dialogEditFormVisible: false,
			testpaperForm: {
				name: "",
				identityCard: "",
				passMark: "",
				score: "",
				testNumber: "",
				testType: "",
				totalScore: "",
				testId: 0,

			},
			queryForm: {
				name: "",
				type: "",
				names:[localStorage.getItem('USERNAME')],
				testName:"",
				addTime: [],
				updateTime: [],
				pageNo: 1, // 当前页码
				pagesize: 100, // 每页记录数
				pagesizes: [10, 20, 50, 100]
			},

			pagetotal: 0,
			pagetotaltemp: 0,
			pickerOptions: {
				shortcuts: [{
					text: '最近一周',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
						picker.$emit('pick', [start, end]);
					}
				}, {
					text: '最近一个月',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
						picker.$emit('pick', [start, end]);
					}
				}, {
					text: '最近三个月',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
						picker.$emit('pick', [start, end]);
					}
				}]
			},
			//
			toolbar:TOOLBAR,
			//员工
			allusers: [],
			//
			members:[],
			// 多选
			multipleSelection: [],
			pagedatatemp: [], // 当前页结果集
			pagedata: [], // 当前页结果集
			loaded: false,
		}
	},
	methods: {
		// 取得所有用户列表
		async getAllUser() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.get('api/user/all');
				if (res.code === 200) {
					this.allusers = res.data;
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('取得所有用户列表请求出错！', e.message);
			}
		
			// 关闭加载框
			loading.close();
		
		},
		// 初始化团队成员列表
		async initMembers() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.get('api/team/member/' + localStorage.getItem('USERID'));
				if (res.code === 200) {
					this.members = res.data;
					if (this.members.length > 0) {
						this.queryForm.userid = this.members[0].id;
					}
				}else {
					this.$message.error(res.message);
				}
			}catch (e) {
				this.$message.error('请求出错！', e.message);
			}
			
			// 关闭加载框
			loading.close();
		},
		entertestlink(index, row) {
			//testId
			var href = TEST + "testid=" + row.id
			window.location.href = href;
		},
		copypaperlink(index, row) {
			//testId
			console.log(row)
			var href = TEST + "testid=" + row.id
			this.teamLink = href;
			this.copy(index, row);
		},
		copy(index, row) {
			this.$copyText(this.teamLink).then(e => {
				this.$message({
					message: '复制成功！',
					type: 'success'
				})
			}, e => {
				this.$message({
					message: '复制失败！',
					type: 'error'
				})
			})
		},
		tableRowClassName({
			row,
			rowIndex
		}) {
			
			if (parseInt(this.pagedata[rowIndex].score)>=parseInt(this.pagedata[rowIndex].passMark)) {
				return 'success-row';
			}else if (parseInt(this.pagedata[rowIndex].score)<parseInt(this.pagedata[rowIndex].passMark)) {
          return 'warning-row';
        }
			return '';
		},
		// 显示侧边栏
		showLeftBar() {
			if (this.leftbar_show)
				return this.leftbar_show = false
			if (!this.leftbar_show)
				return this.leftbar_show = true
		},
		// 表格多选 
		handleSelectionChange(val) {
			this.multipleSelection = val;
		},
		handleRemove(file, filelist) {},

		handleAvatarSuccess(res, file, filelist) {},
		// 检查数据是否有修改
		checkDataChanged() {
			// 比对数据有无发生变化
			for (let key in this.testpaperForm) {
				if (this.testpaperForm[key] != this.testpaperFormOrg[key]) {
					return true;
				}
			}
			return false;
		},

		start() {
			this.queryForm.pageNo = 1
			this.query()
		},

		// 查询
		async query() {
			this.submitDisable = true;
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.post('api/testpaperview/querylist', this.queryForm);
				if (res.code === 200) {
					this.pagedata = res.data;
					this.pagetotal = res.total;
					for (let item in this.pagedata) {
						this.pagedata[item].add_time = this.pagedata[item].add_time
					}
					this.$message.success(res.message);

				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('请求出错！', e.message);
			}
			console.log(this.pagedata)
			// 关闭加载框
			loading.close();
			this.submitDisable = false;
		},
		//修改
		async editconfirm() {
			this.submitDisable = true;
			this.$refs.testpaperForm.validate(async valid => {
				if (valid) {
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res = await axios.put('api/testpaperview/edit', this.testpaperForm);
						// 编辑
						// checkdata = this.checkDataChanged();
						// if (checkdata.length > 0) {

						// } else {
						// 	res = {
						// 		code: 400,
						// 		message: "数据未变化"
						// 	};
						// }
						if (res.code === 200) {
							this.query();
							this.dialogFormVisible = false
							this.dialogEditFormVisible = false
						} else {
							this.$message.error(res.message);
						}

					} catch (e) {
						this.$message.error('请求出错！', e.message);
					}
					// 关闭加载框
					loading.close();
				}
			})
			this.submitDisable = false;
		},



		selectionChange(selection) {
			this.selection = selection
		},


		// 编辑
		edit(index, row) {
			this.action = "edit"
			this.index = index;
			this.dialogEditFormVisible = true;
			this.testpaperForm = deepCopy(row)
			this.testpaperForm.testId = row.id
			console.log(this.testpaperForm)
			// 保留原始数据，方便比对
			this.testpaperFormOrg = deepCopy(row)
		},
		//删除
		async removetestpaper(){
			try {
				let res =await axios.post('api/testpaperview/remove', this.testpaperForm);
				console.log(res)
				if (res.code === 200) {
					this.query();
					this.$message({
					  type: 'success',
					  message: '删除成功!'
					});
				} else {
					this.$message.error(res.message);
				}
			
			} catch (e) {
				this.$message.error('请求出错！', e.message);
			}
			
		},
		//删除
		remove(index, row) {
			this.$confirm('此操作将永久删除该考试信息, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.testpaperForm = deepCopy(row)
				this.testpaperForm.testId = row.id
				this.removetestpaper()

			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},

		// 设定表格当前页
		handleCurrentPageChange(selectedPageNo) {
			this.queryForm.pageNo = selectedPageNo
			// 加载数据页
			this.query()
		},
		//设定每页记录数
		handleSizeChange(val) {
			this.queryForm.pagesize = val
			// 加载数据页
			this.query()
		},


		createStateFilter(queryString) {
			return (state) => {
				return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
			};
		},
		handleSelect(item) {
			console.log(item);
		},

		async initTestTypes() {
			try {
				let res = await axios.get('api/test/inittype');
				if (res.code === 200) {
					this.typeoptions = res.data
				} else {
					this.$toast("题库大分类取得出错。");
				}
			} catch (e) {
				this.$toast("网络连接异常。");
			}
		},
		async initTestSubTypes(type) {
			try {
				let res = await axios.get('api/test/initsubtype/' + type);
				if (res.code === 200) {
					this.smalltypeoptions = res.data
				} else {
					this.$toast("题库小分类取得出错。");
				}
			} catch (e) {
				this.$toast("网络连接异常。");
			}
		},
		changeType(value) {
			this.initTestSubTypes(value);
		}
	},

	async mounted() {
		// 取得查询条件者列表
		this.getAllUser();
		this.initMembers();
		// 自动调整表格高度
		window.onresize = () => {
			return (() => {
				this.tableHeight = window.innerHeight - 180
			})()
		}
		this.initTestTypes();
		this.initTestSubTypes("None");


	}

});
